﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Portal.Web.Areas.Admin.Controllers
{
    public class PermissionController : BaseAdminController
    {
        private readonly IRoleRepository _roleRepository;
        private readonly IMenuInRoleRepository _menuInRoleRepository;
        private readonly IMenuRepository _menuRepository;
        private readonly IActionInRoleRepository _actionInRoleRepository;
        private readonly IActionRepository _actionRepository;
        private readonly IUserRepository _userRepository;
        private readonly IUserInRoleRepository _userInRoleRepository;

        public PermissionController(IRoleRepository roleRepository, IMenuInRoleRepository menuInRoleRepository, IMenuRepository menuRepository, IActionInRoleRepository actionInRoleRepository, IActionRepository actionRepository, IUserRepository userRepository, IUserInRoleRepository userInRoleRepository)
        {
            _roleRepository = roleRepository;
            _menuInRoleRepository = menuInRoleRepository;
            _menuRepository = menuRepository;
            _actionInRoleRepository = actionInRoleRepository;
            _actionRepository = actionRepository;
            _userRepository = userRepository;
            _userInRoleRepository = userInRoleRepository;
        }

        [Description("获取所有Action列表")]
        [ViewPage]
        public ActionResult AllAction()
        {


            return View();
        }

        [Description("获取所有Action列表")]
        public ActionResult GetAllActionJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(ActionPermission.GetAllActionByAssembly().AsQueryable().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }

        public ActionResult AddMangeAction(Action entity)
        {
            var mode = _actionRepository.CreateEntity();
            mode.ActionName = entity.ActionName;
            mode.Area = entity.Area;
            mode.ControllerName = entity.ControllerName;
            mode.Culture = entity.Culture;
           
            return View();
        }

        public ActionResult AddRole(Role entity)
        {
            var model = _roleRepository.CreateEntity();
            model.Founder = CurrentUserName;
            model.Culture = Culture;
            model.Name = entity.Name;
            model.Description = entity.Description;
            model.Sort = entity.Sort;
            _roleRepository.Add(model);
            _roleRepository.Save();
            return View();
        }


        public ActionResult AddMenu(Menu entity)
        {
            var parentModel = _menuRepository.Get().FirstOrDefault(o => o.Id == entity.ParentId);

            var model = _menuRepository.CreateEntity();
            model.Culture = Culture;
            model.Name = entity.Name;
            model.Icon = entity.Icon;
            model.Culture = Culture;
            model.Sort = entity.Sort;
            model.Url = entity.Url;
            model.IsClosed = entity.IsClosed;
            if (parentModel != null)
            {
                model.ParentId = parentModel.Id;
                model.Depth = parentModel.Depth + 1;
            }
            _menuRepository.Add(model);
            _roleRepository.Save();
            return View();
        }


        public ActionResult RoleMange()
        {
            return View();
        }

        public ActionResult GetRoleJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_roleRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }

        public ActionResult MenuMange()
        {
            return View();
        }

        public ActionResult GetMenuJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_menuRepository.Get().AsQueryable().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }


        public ActionResult GetUserJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_userRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }




    }
}
